/*
	Pinkie's Balloon Fight
	Copyright (C) 2012  dancinninja (dancinninjac@gmail.com)

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "Player.h"
#include "Platform.h"
#include "Enemy.h"
#include "Balloon.h"
#include "TexManager.h"

Player::Player(){
	lives = 3; // We start out with 3 balloons

	// Make us bigger
	width = 60;
	height = 60;

	balloons[0].SetOffsets(-width/2, height);
	balloons[0].SetLagMod(-30);
	balloons[1].SetOffsets(width/4, height + 30);
	balloons[1].SetLagMod(0);
	balloons[2].SetOffsets(width/2, height);
	balloons[2].SetLagMod(30);

}

void Player::Update(){

	// ---------- Apply Gravity---------------------------
	if(yAccel > - TERMINAL_VELOCITY - GRAVITY_ACCELERATION){ 
		yAccel -= GRAVITY_ACCELERATION;
	}	
	
	// Regular update
	LiveObject::Update();

	for(int i = 0; i < lives; i++){
		balloons[i].FollowPoint(x, y);
	}
}

void Player::Render(){

	glBindTexture(GL_TEXTURE_2D, texture->obj);
	glLoadIdentity();
	
	glTranslatef(x, y, -1);
	glScalef(width, height, 1);
	glColor4ub(255,255,255,255);

	glBegin(GL_QUADS);
		glTexCoord2f(0.0f, 0.0f); glVertex3f(0.0f, 0.0f, 0.0f);
		glTexCoord2f(1.0f, 0.0f); glVertex3f(1.0f, 0.0f, 0.0f);
		glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0f, 1.0f, 0.0f);
		glTexCoord2f(0.0f, 1.0f); glVertex3f(0.0f, 1.0f, 0.0f);
	glEnd();

	for(int i = 0; i < lives; i++){
		balloons[i].Render();
	}

}

void Player::SetPosition(f32 _x, f32 _y){
	x = _x;
	y = _y;
	// Set the starting points for each balloon
	// This will be recalculated on each "pop":
	for(int i = 0; i < lives; i++){
		balloons[i].SetPosition(_x, _y);
	}
}

int Player::GetLives() const {
	return lives;
}

// Now we're colliding with the enemy!
void Player::CollideWithEnemy(Enemy* enemy){

	// Check for the Vertical section (x-axis)
	if(x + width > enemy->x){
		//  ooo   | 
		//  ooo   | 
		//   |    |
		//   |----|
		//   |    |
		if(x < enemy->x + enemy->width){
			//   |   ooo
			//   |   ooo
			//   |    |
			//   |----|
			//   |    |
			// Check for the Horizontal section (y-axis)
			if(y < enemy->y + enemy->height && y + height > enemy->y){
				//   |    |
				//   |    |
				//   |   ooo
				//   |---ooo
				//   |    |

				// We have hit the enemy! Now to see if we got hit... or they did!

				if(y + height/2 >= enemy->y + enemy->height/2){
					// We hit them!
					enemy->GetHit();

					// Make us go up! Like a jump!
					y = enemy->y + height;
					yAccel += VIRTICAL_JUMP_ACCEL;

					if(enemy->IsAlive()){
						enemy->yAccel /= -HIT_Y_DECEL;	
					}else{
						// Delete the enemy; it's dead.
						
					}
				}else{
					// They hit us.
					GetHit();
					if(!IsAlive()){
						//GAME OVER!
					}
					// We're stil alive, adjust balloons accordingly

					//Make us go Down (like we hit our heads)
					y = enemy->y - height;
					yAccel /= -HIT_Y_DECEL;	
				}
				//return true;
			}
		}
	}
	//return false;
}

